//
//  UIBarButtonItem-Extension.swift
//  SwiftDemo
//
//  Created by liguoqing on 2022/1/19.
//  Copyright © 2022 guoqing.li. All rights reserved.
//

import Foundation
import UIKit

extension UIBarButtonItem {
    // 类方法
    class func createItem(imageName:String, highImageName:String = "", size:CGSize) -> UIBarButtonItem {
        let btn = UIButton()
        btn.setImage(UIImage.init(named: imageName), for: .normal)
        btn.setImage(UIImage.init(named: highImageName), for: .highlighted)
        btn.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
        let barItem = UIBarButtonItem(customView: btn)
        return barItem;
    }
    // 构造函数
    convenience init(imageName:String, highImageName:String = "", size:CGSize = CGSize.zero, viewController:UIViewController, selector:Selector) {
        // 创建btn
        let btn = UIButton()
        // 设置图片
        btn.setImage(UIImage.init(named: imageName), for: .normal);
        if highImageName != ""{
            btn.setImage(UIImage.init(named: highImageName), for: .highlighted)
        }
        // 设置尺寸
        if size != CGSize.zero{
            btn.frame = CGRect(origin: CGPoint.zero, size: size)
        }
        // 点击事件
        btn.addTarget(viewController, action: selector, for: .touchUpInside)
        
        self.init(customView: btn)
    }
}
